package dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.activity_login;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.R;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.activity_tally.HomeActivity;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.database.DatabaseHelper;

public class PasswordLoginActivity extends AppCompatActivity {

    private EditText editText_username;
    private EditText editText_password;
    private DatabaseHelper databaseHelper;
    private SQLiteDatabase sqLiteDatabase;
    private TextView toVerificationLogin;
    private Button button_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_login);

        databaseHelper = new DatabaseHelper(PasswordLoginActivity.this);

        editText_username = findViewById(R.id.editText_username);
        editText_password = findViewById(R.id.editText_password);
        button_login = findViewById(R.id.button_login);
        toVerificationLogin = findViewById(R.id.toVerificationLogin);
        TextView title = findViewById(R.id.appName_password);
        Typeface typeface = Typeface.createFromAsset(getAssets(),"myFont.ttf");
        title.setTypeface(typeface);

        final SpannableStringBuilder style = new SpannableStringBuilder();
        style.append("验证码登录");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Intent intent = new Intent(PasswordLoginActivity.this, VerificationLoginActivity.class);
                startActivity(intent);
            }
        };
        style.setSpan(clickableSpan, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.parseColor("#8BC34A")), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        toVerificationLogin.setMovementMethod(LinkMovementMethod.getInstance());
        toVerificationLogin.setText(style);

        button_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                String username_input = editText_username.getText().toString();
                String password_input = editText_password.getText().toString();
                //如果输入框为空
                if (username_input.equals("") || password_input.equals("")) {
                    Toast.makeText(PasswordLoginActivity.this, "用户名/手机号或密码不能为空", Toast.LENGTH_SHORT).show();
                }
                //输入框不为空
                else {
                    //根据手机号/用户名输入框内容查询数据库获取手机号和密码
                    sqLiteDatabase = databaseHelper.getReadableDatabase();
                    Cursor cursor = sqLiteDatabase.query("users", new String[]{"password,phone"}, "username=? OR phone=?",
                            new String[]{username_input, username_input}, null, null, null);
                    if (cursor.moveToFirst()) {
                        String passwordInDatabase = cursor.getString(0);
                        String phoneInDatabase = cursor.getString(1);
                        //数据库中用户名或手机号对应的密码与输入密码匹配，跳转至主界面
                        if (MD5Utils.md5Password(password_input).equals(passwordInDatabase)) {
                            Toast.makeText(PasswordLoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            intent = new Intent(PasswordLoginActivity.this, HomeActivity.class);
                            intent.putExtra("phone_toHA", phoneInDatabase);
                            startActivity(intent);
                        }
                        //数据库中用户名对应的密码与输入密码不匹配，提示错误
                        else {
                            Toast.makeText(PasswordLoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                    //数据库中没有输入用户名和手机号的对应项，提示用户不存在
                    else {
                        Toast.makeText(PasswordLoginActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
                    }
                    cursor.close();
                }
            }
        });
        Log.d("tag","PasswordLoginActivity create");
    }

    @Override
    public void finish() {
        super.finish();
        Log.d("tag","PasswordLoginActivity finish");
    }
}